package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.OrderDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderAddress;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private JedisPool jedisPool;
    @Override
    public PageResult selectAllByName(QueryPageBean queryPageBean) {
        try {
            //获取参数
            String queryString = queryPageBean.getQueryString();
            //使用分页插件
            PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
            Page<OrderAddress> orderAddresses = orderDao.selectAllByName(queryString);
            return new PageResult(orderAddresses.getTotal(),orderAddresses.getResult());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Result add(OrderAddress orderAddress) {
        try {
            //添加数据
            orderDao.add(orderAddress);
            //获取图片
            String imageUrl = orderAddress.getImageUrl();
            //存入数据库缓存中
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,imageUrl);
            return new Result(true,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加失败");
        }
    }

    @Override
    public Result updateOne(OrderAddress orderAddress) {
        try {
            //修改预约地址
            if (orderAddress!=null){
                orderDao.updateOne(orderAddress);
            }
            return new Result(true,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改失败");
        }
    }

    @Override
    public Result deleteById(int id) {
        try {
            //查询是否管理预约
            List<Order>  orderList = orderDao.findById(id);
            if (orderList==null||orderList.size()==0){
                //没有关联数据 直接删除
                orderDao.deleteById(id);
                return new Result(true,"删除成功");
            }
            //中间表有数据
            for (Order order : orderList) {
                //判断是否就诊
                String orderStatus = order.getOrderStatus();
                if (orderStatus.equals("未到诊")){
                    return new Result(false,"该地址已经有人预约");
                }
            }
            //说明已到诊 直接删除
            orderDao.deleteById(id);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }
}
